# exceptions.py
from venv import logger

from fastapi import HTTPException, status, FastAPI, Request
from starlette.responses import JSONResponse


class NotFoundError(HTTPException):
    def __init__(self, detail: str = "Resource not found"):
        super().__init__(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=detail
        )

class BadRequestError(HTTPException):
    def __init__(self, detail: str = "请求参数错误"):
        super().__init__(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=detail
        )

class UnauthorizedError(HTTPException):
    def __init__(self, detail: str = "用户token已过期"):
        super().__init__(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=detail
        )


class ForbiddenError(HTTPException):
    def __init__(self, detail: str = "用户无权限"):
        super().__init__(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=detail
        )

class InternalServerError(HTTPException):
    def __init__(self, detail: str = "服务器内部错误"):
        super().__init__(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=detail
        )

class ExistError(HTTPException):
    def __init__(self, detail:str = '用户已存在'):
        super().__init__(
            status_code=status.HTTP_409_CONFLICT,
            detail=detail
        )


def register_exception_handlers(app: FastAPI):
    @app.exception_handler(NotFoundError)
    async def not_found_exception_handler(request: Request, exc: HTTPException):
        logger.error(f"HTTP Error: {exc.detail}")
        return JSONResponse(
            status_code=exc.status_code,
            content={"code": exc.status_code,"message": exc.detail}
        )


    @app.exception_handler(UnauthorizedError)
    async def unauthorized_exception_handler(request: Request, exc: HTTPException):
        logger.error(f"HTTP Error: {exc.detail}")
        return JSONResponse(
            status_code=exc.status_code,
            content={"code": exc.status_code,"message": exc.detail}
        )

    @app.exception_handler(BadRequestError)
    async def bad_request_exception_handler(request: Request, exc: HTTPException):
        logger.error(f"HTTP Error: {exc.detail}")
        return JSONResponse(
            status_code=exc.status_code,
            content={"code": exc.status_code,"message": exc.detail}
        )


    @app.exception_handler(InternalServerError)
    async def internal_server_exception_handler(request: Request, exc: HTTPException):
        logger.error(f"HTTP Error: {exc.detail}")
        return JSONResponse(
            status_code=exc.status_code,
            content={"code": exc.status_code,"message": exc.detail}
        )


    @app.exception_handler(ExistError)
    async def user_exists_exception_handler(request: Request, exc: HTTPException):
        logger.error(f"HTTP Error: {exc.detail}")
        return JSONResponse(
            status_code=exc.status_code,
            content={"code": exc.status_code,"message": exc.detail}
        )